//
//  TabItem.m
//  SinaWeiBo
//
//  Created by 姚建强 on 15-2-11.
//  Copyright (c) 2015年 com.yjq. All rights reserved.
//

#import "TabItem.h"
#define TEXTHIGHT 15
#define SIGNHEIGHT 15
#define SIGNWIDTH  15
@interface TabItem(){
    UIButton* signView;
}
@end

@implementation TabItem
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
-(id)initWithFrame:(CGRect)frame Model:(TabItemModel*)model{
    self=[super initWithFrame:frame];
    if (self) {
        [self setImage:[UIImage imageNamed:model.imagePath] forState:UIControlStateNormal];
        [self setTitle:model.title forState:UIControlStateNormal];
        [self setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
        [self setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
        [self.titleLabel setTextAlignment:NSTextAlignmentCenter];
        [self.titleLabel setFont:[UIFont systemFontOfSize:12]];
        [self.imageView setContentMode:UIViewContentModeScaleAspectFit];
        [self setBackgroundColor:[UIColor whiteColor]];
        
        [self.layer setBorderWidth:0.5];//设置边框
        [self.layer setCornerRadius:1.5];//设置圆角
    }
    return self;
}
/*!
 *  设置item显示分类推送消息数
 *
 *  @param signNum 分类消息数目
 */
-(void) setSignViewWithNum:(NSInteger*) signNum{
    if (signView==nil) {
        CGRect frame=[self.imageView frame];
        frame=CGRectMake(frame.size.width-SIGNWIDTH-20, 0, SIGNWIDTH, SIGNHEIGHT);
        signView=[[UIButton alloc]initWithFrame:frame];
        [signView setTitle:[[NSString alloc] initWithFormat:@"%ld",signNum] forState:UIControlStateNormal];
        [signView setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [signView setBackgroundImage:[UIImage imageNamed:@"userinfo_vip_background@2x.png"] forState:UIControlStateNormal];
        [signView setAdjustsImageWhenHighlighted:NO];//设置不高亮显示
        [signView.titleLabel setTextAlignment:NSTextAlignmentCenter];
        [signView.titleLabel setFont:[UIFont systemFontOfSize:12]];
        
        [self addSubview:signView];
    }else{
        [signView setTitle:[[NSString alloc] initWithFormat:@"%ld",signNum] forState:UIControlStateNormal];
        
    }
    if (signNum==0) {
        [signView setHidden:YES];
    }
    else{
        [signView setHidden:NO];
    }
    
}
#pragma mark -重写title布局方法
-(CGRect)titleRectForContentRect:(CGRect)contentRect{
    //设置titleLabel显示位置
    contentRect.origin.y=contentRect.size.height-TEXTHIGHT;
    //设置titleLabel高度
    contentRect.size.height=TEXTHIGHT;
    return contentRect;
    
}
#pragma mark -重写image布局方法
-(CGRect)imageRectForContentRect:(CGRect)contentRect{
    //设置图片高度
    contentRect.origin.y=contentRect.origin.y+5;
    contentRect.size.height=contentRect.size.height-TEXTHIGHT-5;
    
    return contentRect;

    
}


-(void) TabItemSeletedIndex:(NSInteger*) index{
    NSLog(@"%d",index);
}
@end
